package cn.com.pxto.dao;

import java.util.List;

import cn.com.pxto.commons.SearchParam;
import cn.com.pxto.commons.pagesupport.PageInfo;
import cn.com.pxto.dao.pagesupport.hibernate.HibernatePageDaoSupport;
import cn.com.pxto.model.Guest;

public class GuestDAO extends HibernatePageDaoSupport {
	public Guest getGuest(Integer gid){
		return (Guest)getTemplate().get(Guest.class, gid);
	}
	
	public void saveGuest(Guest guest){
		getTemplate().save(guest);
	}
	
	public void deleteGuest(Integer gid){
		String HQL="from Guest as g where g.gid="+gid;
		getTemplate().delete(HQL);
	}
	
	public void updateGuest(Guest guest){
		getTemplate().saveOrUpdate(guest);
	}
	
	@SuppressWarnings("unchecked")
	public List<Guest> listGuest(){
		String HQL="select g from Guest as g order by g.id desc";
		return getTemplate().find(HQL);
	}
	
	@SuppressWarnings("unchecked")
	public List<Guest> listGuest(PageInfo pageInfo){
		String HQL="select g from Guest as g order by g.id desc";
		return getTemplate().pagedFind(HQL, pageInfo);
	}
	@SuppressWarnings("unchecked")
	public List<Guest> listGuestForNormal(int userId,String sort,PageInfo pageInfo){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select g from Guest as g where g.userId= " );
		HQL.append(userId);
		HQL.append(" order by g.");
		HQL.append(sort);
		HQL.append(" desc ");
		return getTemplate().pagedFind(HQL.toString(), pageInfo);
	}
	@SuppressWarnings("unchecked")
	public List<Guest> listGuestForManager(String sort,PageInfo pageInfo){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select g from Guest as g " );
		HQL.append(" order by g.");
		HQL.append(sort);
		HQL.append(" desc ");
		return getTemplate().pagedFind(HQL.toString(), pageInfo);
	}
	@SuppressWarnings("unchecked")
	public List<Guest> getGuest(String sort,SearchParam[] params,PageInfo pageInfo){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select g from Guest as g where (1=1)");
		HQL.append(SearchParam.getSimpleWhereSentence(params,"g"));
		HQL.append(" order by g.");
		HQL.append(sort);
		List<Guest> list=getTemplate().pagedFind(HQL.toString(), SearchParam.getSearchValues(params), pageInfo);
		return list;
	}
	@SuppressWarnings("unchecked")
	public List<Guest> getGuest(SearchParam[] params,PageInfo pageInfo){
		StringBuffer HQL=new StringBuffer();
		HQL.append("select g from Guest as g where (1=1)");
		HQL.append(SearchParam.getSimpleWhereSentence(params,"g"));
		HQL.append(" order by g.returnVisitTime desc , g.importantType desc ");
//		HQL.append(sort);
		List<Guest> list=getTemplate().pagedFind(HQL.toString(), SearchParam.getSearchValues(params), pageInfo);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Guest> listGuestByTel(String tel){
		String HQL="select g from Guest as g where g.tel like '%"+tel+"%' order by g.logonDate desc";
//		String HQL="select g from Guest as g where g.tel = '"+tel+"' ";
		return getTemplate().find(HQL);
//		SearchParam[] params = new SearchParam[1];
//		params[0] = new SearchParam("tel",tel,SearchParam.SEARCH_PARAM_TYPE_STRING_INDISTINCT);
//		StringBuffer HQL=new StringBuffer();
//		HQL.append("select g from Guest as g where (1=1)");
//		HQL.append(SearchParam.getSimpleWhereSentence(params,"g"));
//		HQL.append(" order by g.logonDate");
//		List<Guest> list=getTemplate().find(HQL.toString(), SearchParam.getSearchValues(params));
//		return list;
		
	}
	
	
}
